// /rule/addRuleInfo.do
document.write('<script type="text/javascript" src="../static/js/back/utils.js"></script>');
document.write('<script type="text/javascript" src="../static/js/back/ajax.js"></script>');
var _url_ = '../shop/addShopInfo.do';
var _url_edit = '../shop/updateShopInfo.do';
var _map = {};
///首页
var _pageNum = 1;
var _type ;
  $(function(){
//	  console.log(getCurrentPostionByIp())  
	  _type = getUrlParam("type");
	  if(!_type){
		  var rePath = getUrlRelativePath();
		  console.log(rePath);
		  if(rePath.indexOf("-add")>0){
			  _type="add";
		  }
	  }
	  console.log(_type)
	  if(_type === "add"){
		  //getRuleListAdd();
		 /* $("input[name='userPwd']").parent().prev().find('span').show();
		  $("input[name='userPwd']").attr('lay-verify','required|pass');*/
	  }else if(_type === "edit"){
		  $('.x-nav').remove();
		  //getRuleListAdd();
		  showEditData();
		  $("input[name='userPwd']").parent().prev().find('span').hide();
		  $("input[name='userPwd']").attr('lay-verify','').parent().parent().find('.edit-in').show();
	  }else{
		  
		  getList();
	  }
  })
$(function(){
	  $("body").on("click",".mypage span",function(){
		  _pageNum = $(this).attr("data-status");
		  getList();
	  })
	  
	  $("#addressInfo").change(function(){
		  //xcity_change();
	  });
	  
	  $(".search-btn").click(function(){
		 var tit =  $("#ser-title").val();
		 _map["shopTitle"] = tit;
		 var start =  $("#startTime").val();
		 var end =  $("#endTime").val();
		 _map['startTime'] = start;
		 var end =$("#end").val();
		  if(end){
			  end =  addDate(end,1);
		  }
		 _map['endTime'] = end;
		 var _stype =  $("#shopType").val();
		 _map['shopType'] = _stype;
		 _pageNum = 1;
		 getList();
	  });
	  
	  $("input[name='userPwd']").keyup(function(){
		  if(_type==="edit"){
			  if($(this).val()){
				  $(this).attr('lay-verify','required|pass');
			  }else{
				  $(this).attr('lay-verify','');
			  }
		  }
	})
	  
	  
});
  function getList(){
	  
	  _map["pageNum"] = _pageNum;
	  
	  _ajax_post_token('../shop/getShopsList',_map,function(res){
		  console.log(res.returnObject);
		  if(!res.returnObject){
			  $(".layui-table tbody").html("");
			  $(".no-more-data").show();
			  return;
		  }else{
			  $(".no-more-data").hide();
		  }
		  var html = "";
		  var status = "";
		  var statusHtml = "";
		  var statusHtmlTile = "";
		  var type = "";
		  var support = ""; //商家支持
		  if(!res.returnObject){
			  html = '<div class="layui-form-mid layui-word-aux no-more-data">暂无更多数据</div>';
			  $(".layui-table ").after(html);
			  return;
		  }
		  showPageInfo(res.returnObject);
		  $(".x-right").text("共有数据："+res.returnObject.total +" 条");
		  $.each(res.returnObject.list,function(i,v,arr){
			  console.log(v);

			  if(v.shopStatus == 1){
				  status = "营业中";
				  //判断是否在营业时间内
				  if(v.openHours){
					  var tims = v.openHours.split(",");
			          console.log(tims);
			          var tts = false;
			          for (var k = 0; k < tims.length; k++) {
			            tiflag = timeRange(tims[k].split("-")[0], tims[k].split("-")[1]);
			            if (tiflag) {
			            	tts = true;
			                break;
			            }
			          }
			          
			          if(!tts){
			        	  status = "待开始营业"
			          }
		          
				  }
				  
			  }else if(v.shopStatus == 2){
				  status = '待开始营业';
			  }else if(v.shopStatus == 3){
				  status = '已关店';
			  }
			  
			  if(v.shopType == 1){
				  type='美食';
			  }else{
				  type='超市';
			  }
			  
			  
			  if(v.shopSupport){
				  var  sups = v.shopSupport.split(',');
				  for (var k = 0; k < sups.length; k++) {
					  var sup = sups[k];
					  if(sup == 1){
						  support += "<span class=' shop-sup mei' >外卖</span>";
					  }else if(sup == 2){
						  support += "<span class=' shop-sup tang' >堂食</span>";
					  }
				  }
			  }else{
				  support = "-";
			  }
			  
			  html+=' <tr><td>'
//    	             +' <div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id="'+v.id+'" ><i class="layui-icon">&#xe605;</i></div>'
//    	             +' </td>                                                                                                                  '
//    	             +' <td>'
    	             +(i+res.returnObject.startRow)+'</td>                                                                                                             '
    	             +' <td align="center"><div style="height:80px;width:80px;"><img  src="'+v.shopLogo+'" style="width:100%;height:100%" ></div></td>                                                                                           '
    	             +' <td>'+v.shopTitle+'</td>                                                                                                      '
    	             +' <td>'+type+'</td>                                                                                                      '
    	             +' <td>'+support+'</td>';
			  		 if( CompareDate(v.serverEndTime,new Date()) ){
			  			 html+=' <td>'+v.serverEndTime+'</td>  ';
			  		 }else{
			  			 html+=' <td class="out-time">'+v.serverEndTime+'</td>  ';
			  		 }
    	             html +=' <td>'+(v.isRecommend=="1"?"<i class=\"iconfont x-blue-1\"> 推荐</i>":"-")+'</td>                                                                                                      '
    	             +' <td>'+status+'</td>                                                                                                      '
    	             +' <td class="td-manage">                                                                                                 '
    	             +'   <a title="编辑" onclick="x_admin_show(\'修改门店信息 - '+v.shopTitle+'\',\'./shop-add.html?type=edit&id='+v.id+'\',\'\',\'\',true)"  href="javascript:;">                                         '
    	             +'     <i class="iconfont">修改</i> </a>                                                                                '
    	             +'   <a  data-status="'+(v.isRecommend == "1"?"0\" title=\"取消推荐\"":"1\" title=\"推荐\"")+'" onclick="member_tui(this,\''+v.id+'\')" href="javascript:;">                                         '
    	             +'     <i class="iconfont">'+(v.isRecommend=="1"?"取消推荐":"推荐")+'</i> </a>                                                                                '
    	             +'   <a title="登陆" data-status = "'+v.userAccount+'" data-shopt="'+v.shopTitle+'" onclick="member_log(this,\''+v.id+'\')" href="javascript:;">                                         '
    	             +'     <i class="iconfont ">登陆</i> </a>                                                                                '
//    	             +'   <a title="删除" onclick="member_del(this,\''+v.id+'\')" href="javascript:;">                                         '
//    	             +'     <i class="iconfont icon-delete x-red"></i> </a>                                                                                '
    	             +'     </td> </tr>';
    	             support = "";
		  })
		  $(".layui-table tbody").html(html);
	  })
  }
      
  function getRuleListAdd(){
	  _ajax_post_token('../rule/getRuleGroupList',{pageSize:100},function(res){
		  console.log(res);
		  var list =  res.returnObject;
		  var html = "";
		  $.each(list.list,function(i,v,arr){
    		  if(list){
    			 html+='<option value="'+v.id+'">'+v.title+'</option>';
    		  }
		  })
		  $(".ruleIds").append(html);
	  })
  }
  function showEditData(){
	  _url_ = _url_edit;
	  var _id = getUrlParam("id");
	  $("#paramKey").val(_id);
	  console.log(_id);
	  _ajax_post_token('../shop/getShopById',{userType:1,id:_id},function(res){
		  console.log(res);
		  _showData = res.returnObject;
	  });
  }    
      
  /*用户-删除*/
  function member_del(obj,_id){
      layer.confirm('确认要删除吗？',function(index){
          //发异步删除数据
    	  _url_ =_url_edit;
    	  _ajax_post_token(_url_,{shopStatus:0,id:_id},function(res){
    	      	console.log(res);
    	      	if(res.resultCode == 1000){
    	      		getList();
    	      	  layer.msg('删除成功!',{icon:1,time:2000});
    	      	}else{
    	      		layer.msg('删除失败!',{icon:2,time:2000});
    	      	}
    	      });
      });
  }
  /*用户-登陆*/
  function member_log(obj,_id){
	  layer.confirm('确认要登陆吗？',function(index){
		  //发异步删除数据
		  setCookie("shop_id",_id);
		  setCookie("acc",$(obj).attr("data-status"));
		  setCookie("shopTitle",$(obj).attr("data-shopt"));
		  parent.window.location.reload();
		  setCookie("zi","1");
	  });
  }
  /*用户-是否推荐*/
  function member_tui(obj,_id){
	  	 var _tui = $(obj).attr("data-status");
		  //发异步删除数据
		  _url_ =_url_edit;
		  _ajax_post_token("../shop/updateShopInfoBySel.do",{isRecommend:_tui,id:_id},function(res){
			  console.log(res);
			  if(res.resultCode == 1000){
				  getList();
				  layer.msg('操作成功!',{icon: 1,time:1000});
			  }else{
				  layer.msg('操作失败!',{icon:2,time:2000});
			  }
		  });
  }
  
  function test(){
	 /* disAddBtn();
	  parent.closeTopTab();*/
	  //celDisBtnCloseTab('shop-list.html');
	  //parent.clickMenuOnChild('shop-list.html');
  }
 
  function updateInfo(data){
	  disAddBtn();
	  //发异步，把数据提交给后台
	  console.log(_url_);
      _ajax_post_token(_url_,data,function(res){
      	console.log(res);
      	if(_type === 'add'){
      		if(res.resultCode == 1000){
      			getList();
      			layer.msg('添加成功!请等待管理员审核',{icon:1,time:2000},function(){
      			  celDisBtnCloseTab('shop-list.html');
      		  });
      			// 获得frame索引
      		}else if(res.resultCode == 1008){
	      		layer.msg('登陆账号已存在!',{icon:2,time:2000});
      		}else{
      			layer.msg('操作失败!',{icon:2,time:2000});
      		}
      	}else{
      		if(res.resultCode == 1000){
      			parent.getList();
      			parent.layer.msg('操作成功!',{icon:1,time:2000});
      			x_admin_close();
      			// 获得frame索引
      		}else{
      			layer.msg('操作失败!',{icon:2,time:2000});
      		}
      	}
      	celDisAddBtn();	
      });
  }
 
      
  function delAll (argument) {
      var data = tableCheck.getData();
      console.log(data.join(","));
      layer.confirm('确认要删除已选择的数据吗？',function(index){
          //捉到所有被选中的，发异步进行删除
    	  _ajax_post_token(_url_edit,{remAllIds:data.join(",")},function(res){
    	      	console.log(res);
    	      	if(res.resultCode == 1000){
    	      	  getList();
    	      	  layer.msg('操作成功!',{icon:1,time:2000});
    	          // 获得frame索引
    	      	}else if(res.resultCode == 1008){
    	      		layer.msg('登陆账号已存在!',{icon:2,time:2000});
    	      	}else{
    	      		layer.msg('操作失败!',{icon:2,time:2000});
    	      	}
    	      });
      });
    }      
      
      
      
      
      
//添加 和 修改窗口     